﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class RoleBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IRoleDA RoleDA = DataAccess.CreateRoleDA();

        public RoleBLL()
        {
            this.pubParasInfo = null;
            this.dataBaseID = new List<string> { "SYSADM." };
        }

        public RoleBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public RoleBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> roleIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<RoleInfo> roleInfos = new List<RoleInfo>();
                RoleInfo roleInfo = null;
                foreach (string id in roleIDS)
                {
                    roleInfo = RoleDA.GetRoleByID(dataBase, tran, id, this.dataBaseID[0]);
                    roleInfos.Add(roleInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (RoleDA.DeleteRole(dataBase, tran, roleIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < roleIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<RoleInfo>(dataBase, tran, roleInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ROLE", this.dataBaseID[0]);
                        temp = temp + roleIDS[i] + ",";
                    }
                    return temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return RoleDA.GetAllRole(this.dataBaseID[0]);
        }

        public object GetInfoByID(string RoleID)
        {
            return RoleDA.GetRoleByID(RoleID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            RoleQueryEntity roleQuery = obj as RoleQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(roleQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + roleQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(roleQuery.RoleID))
            {
                sqlWhere.Add(" ROLE_ID " + roleQuery.RoleID);
            }
            if (!string.IsNullOrEmpty(roleQuery.RoleName))
            {
                sqlWhere.Add(" ROLE_NAME " + roleQuery.RoleName);
            }
            return RoleDA.GetRoleByQueryList(sqlWhere, roleQuery, "sysadm.");
        }

        public string Insert(object obj)
        {
            string RoleID = string.Empty;
            try
            {
                RoleInfo role = obj as RoleInfo;
                if (RoleDA.CheckRoleIDUnique(role.RoleID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F1100_001", this.pubParasInfo.UserLanguage, new List<string> { role.RoleID });
                }
                role.UpdatedBy = role.CreatedBy;
                role.CreatedDate = DateTime.Now;
                role.UpdatedDate = DateTime.Now;
                if (RoleDA.InsertRole(role, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<RoleInfo>(null, role, AuditAction.Insert, this.pubParasInfo.UserID, "ROLE", this.dataBaseID[0]);
                    RoleID = role.RoleID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return RoleID;
        }

        public string Update(object obj)
        {
            string RoleID = string.Empty;
            try
            {
                RoleInfo role = obj as RoleInfo;
                role.UpdatedDate = DateTime.Now;
                RoleInfo oldRoleInfo = RoleDA.GetRoleByID(role.RoleID, this.dataBaseID[0]);
                if (RoleDA.UpdateRole(role, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<RoleInfo>(oldRoleInfo, role, AuditAction.Update, this.pubParasInfo.UserID, "ROLE", this.dataBaseID[0]);
                    RoleID = role.RoleID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return RoleID;
        }
    }
}

